package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result list(CourseDto courseDto){
        log.info("用户列表查询:{}",courseDto);
        PageResult<Course> pageResult = courseService.list(courseDto);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除用户:{}",id);
        courseService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Course course){
        log.info("添加课程:{}",course);
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        Course course =courseService.getById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改:{}",course);
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getlist(){
        log.info("获取所有课程:");
        List<Course> courseList = courseService.getAll();
        return Result.success(courseList);
    }

    @GetMapping("/subject/{subject}")
    public Result getList(@PathVariable Integer subject)
        {
            log.info("获取课程:{}",subject);
            List<Course> courseList = courseService.getList(subject);
            return Result.success(courseList);
        }
}
